package Unittests;

import java.util.ArrayList;

import junit.framework.TestCase;

import org.junit.Before;
import org.junit.Test;

import Controllers.ClientController;
import Controllers.LoginController;
import Controllers.ServerController;
import Entities.ClientEntity;
import Entities.LoginEntity;
import Entities.ServerEntity;

public class LoginTest extends TestCase{
	
	
	private ArrayList<LoginEntity> users;
	//validate input
	private LoginController logincontroller;
	@Before
	public void setUp() throws Exception {
		
		//creating users list
		users=new ArrayList<LoginEntity>();
		
		//adding new users
		users.add(new LoginEntity("1","111","0","ME"));
		users.add(new LoginEntity("2","222","1","MM"));
		users.add(new LoginEntity("3", "333", "0", "CRE"));
		users.add(new LoginEntity("4", "444", "0", "SM"));
		
		//initialize controller
		logincontroller = new LoginController(users);
	}


	
	@Test
	public void testGetIndexOfUser()
	{
		//Existing user
		String eUid="1";
		assertTrue(logincontroller.getIndexOfUser(eUid)!=-1);
				
		//Unregistered user
		String id="12";
		assertTrue(logincontroller.getIndexOfUser(id)==-1);
	}
	@Test
	public void testIsUserExists()
	{
		//Existing user
		String eUid="1";
		assertTrue(logincontroller.isUserExists(eUid));
		
		//Unregistered user
		String id="12";
		assertFalse(logincontroller.isUserExists(id));
	}
	@Test
	public void testCheckPassword()
	{
		String eUid="1";
		//Existing user and correct password
		String ePassword="111";
		assertTrue(logincontroller.checkPassword(eUid, ePassword));
		
		//Existing user and incorrect password
		String password="222";
		assertFalse(logincontroller.checkPassword(eUid, password));
		
		//Unregistered user
		String id="12";
		assertFalse(logincontroller.checkPassword(id, password));
		
	}
	@Test
	public void testGetUserInfo()
	{
		//Existing user
		String eUid="1";
		assertTrue(logincontroller.getUserInfo(eUid).equals(users.get(0)));
						
		//Unregistered user
		String id="12";
		assertTrue(logincontroller.getUserInfo(id)==null);
	}
	@Test
	public void testIsUserLoggedIn()
	{
		//Not logged user
		String nlUid="1";
		assertFalse(logincontroller.isUserLoggedIn(nlUid));
		
		//Logged user
		String lUid="2";
		assertTrue(logincontroller.isUserLoggedIn(lUid));
	}
	@Test
	public void testChangeLoginStatus()
	{
		//Not logged user
		String nlUid="1";
		assertTrue(logincontroller.changeLoginStatus(nlUid));
		
		//Logged user
		String lUid="2";
		assertFalse(logincontroller.changeLoginStatus(lUid));
	}
	

}
